home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJSRC111.ZIP / go32 / ed / wild.c < prev   
C/C++ Source or Header  |  1993-06-26  |  2KB  |  72 lines

  1. /* This is file WILD.C */
  2. /*
  3. ** Copyright (C) 1993 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. #include <string.h>
  16.  
  17. int wild(char *pattern, char *string)
  18. {
  19.   int nlit;
  20.   while (*pattern)
  21.   {
  22.     switch (*pattern)
  23.     {
  24.       case '*':
  25.         pattern++;
  26.         if (*pattern == 0)
  27.           return 1;
  28.         nlit=0;
  29.         while ((pattern[nlit] != 0)
  30.             && (pattern[nlit] != '*')
  31.             && (pattern[nlit] != '?') )
  32.           nlit++;
  33.         while (1)
  34.         {
  35.           if (strncmp(string, pattern, nlit) == 0)
  36.             break;
  37.           string++;
  38.           if (*string == 0)
  39.             return 0;
  40.         }
  41.         break;
  42.       case '?':
  43.         if (*string == 0)
  44.           return 0;
  45.         pattern++;
  46.         string++;
  47.         break;
  48.       default:
  49.         if (*pattern != *string)
  50.           return 0;
  51.         pattern++;
  52.         string++;
  53.         break;
  54.     }
  55.   }
  56.   if (*string)
  57.     return 0;
  58.   return 1;
  59. }
  60.  
  61. #ifdef DEBUG
  62. main(int argc, char **argv)
  63. {
  64.   int i;
  65.   if (argc < 3)
  66.     return 1;
  67.   for (i=2; argv[i]; i++)
  68.     printf("%s %s %d\n", argv[1], argv[i], wild(argv[1], argv[i]));
  69.   return 0;
  70. }
  71. #endif
  72.